package cmich.cps.svn.hooks;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * The Class Main.
 * @author Jonathan Kissinger <kissi1j@cmich.edu>
 */
public class Main {
	
	/** The Constant LOGGER. */
	private final static Logger LOGGER = Logger.getLogger(Main.class.getName());

	/**
	 * The main method.
	 *
	 * @param args the arguments
	 */
	public static void main(String[] args) {
		final long startTime = System.nanoTime();
		MyLogger.setup(Level.INFO, Level.FINEST, args[1]);
		LOGGER.config("===================================New " + args[0] + "commit===================================");
		Settings settings = new Settings(args[0], args[1], args[2]);
		if(settings.isProf()) {
			System.exit(0);
		}
		settings.setDirectories();
		for(int i=0;i<settings.numDirectories();i++) {
			settings.setProperties(settings.getDirectory(i));
			if(args[0].equalsIgnoreCase("pre")) {
				settings.preCommit(settings.getDirectory(i));
			}
			else if(args[0].equalsIgnoreCase("post")) {
				PostCommit post = new PostCommit(settings.getDirectory(i), settings);
				if(post.doFilesExist()) {
					LOGGER.finer("Files exist.");
					if(post.doTestsExist()) {
						LOGGER.finer("Tests exist.");
						post.compileFiles();
						post.runTests();
						post.cleanUp();
					}
					else {
						LOGGER.info("Tests do not exist, assuming professor doesn't want assignment unit tested and exiting normally.");
						System.exit(0);
					}
				}
				else {
					LOGGER.info("No java files in student directory to test, exiting normally.");
					System.exit(0);
				}
			}
		}
		LOGGER.info("It took " + ((double) (System.nanoTime() - startTime) / 1000000000.0) + " seconds to execute this hook.");
	}
}
